<template>

<el-dialog
    :close-on-click-modal="false"
    destroy-on-close
    :show-close="false"
    width="30%"
    v-model="ToOfficialSTATUS"
    title="员工转正"
  >
  <!-- <el-form  ref="ruleFormRef" :model="formData" :rules="formRules" size="small" :inline="false" label-width="100px">
    <el-form-item label="主题" prop="title">
      <el-input v-model="formData.title" placeholder="请填写主题" maxlength="32" size="small"/>
    </el-form-item>
    <el-form-item label="员工" prop="staffId">
      <el-select v-model="formData.staffId" placeholder="请选择员工" filterable size="small" @change="staffChange">
        <el-option v-for="item in staffList" :key="item.id" :value="item.id" :label="item.name"/>
      </el-select>
    </el-form-item>
    <el-form-item label="部门" prop="oaDeptId">{{ dept && dept.name ? dept.name : '-' }}</el-form-item>
    <el-form-item label="职务" prop="postId">{{ post && post.name ? post.name : '-' }}</el-form-item>
    <el-form-item label="职位" prop="oaPositionId">
      <span>{{ position && position.name ? position.name : '-' }}</span>
      <span style="margin-left: 20px">职级 {{ position && position.gradeName ? position.gradeName : '-' }}</span>
    </el-form-item>
    <el-form-item label="入职日期" prop="occurDate">{{ formData.occurDate }}</el-form-item>
    <el-form-item label="试用期" prop="trialTerm">
      <span v-for="item in dictMap['StaffTrialLimit']" :key="item.value" v-show="item.value == formData.trialTerm">{{ item.name }}</span>
    </el-form-item>
    <el-form-item label="实际转正日期" prop="endDate">
      <el-date-picker v-model="formData.endDate" type="date" placeholder="请选择实际转正日期" size="small" value-format="YYYY-MM-DD" @change="dateChange"/>
    </el-form-item>
    <el-form-item label="备注信息" prop="remark">
      <el-input v-model="formData.remark" type="textarea" placeholder="请填写备注信息" :rows="3" size="small"/>
    </el-form-item>
    <el-form-item label="优先度">
      <el-radio-group v-model="formData.priority" size="small">
        <el-radio :label="0" size="small">普通</el-radio>
        <el-radio :label="1" size="small">高</el-radio>
      </el-radio-group>
    </el-form-item>
  </el-form> -->
    <to-offcial-form ref="form" :selecteTime="thisTime"></to-offcial-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button size="large" @click="close(form)">取消</el-button>
        <el-button @click="submit(form)" type="primary" size="large">确认</el-button>
      </div>
    </template>
  </el-dialog>


</template>

<script>
// 转正
import {onMounted, reactive, toRefs,ref} from 'vue'
import ToOffcialForm from '/@/views/matter/components/ToOffcialForm.vue'
// import {listByState, getStaffById} from '/@/api/oa/staff'
// import {getDictByCodes} from '/@/api/sys/dict'
// import {formatDate} from '/@/utils/formatTime'
// import { homeApi } from "/@/api/home/index";
// import { format } from "/@/views/home/component/lib/utils";

// const homeCalendar = homeApi();
export default {
  name: 'ToOfficial',
  props: {
    ToOfficialSTATUS: {
      type: Boolean,
      default: true,
    },
    idw: {
      type: String,
      default: "",
    },
    thisTime: {
      type: String,
      default: "",
    },
  },
  components: {
    ToOffcialForm
  },
  emits: ["ToOfficialCloseCloseClose"],
  setup() {
    const form = ref()
    const state = reactive({
      // formData: {
      //   staffId: null,
      //   title: '员工转正',
      //   oaDeptId: null,  // oa系统部门id
      //   postId: null,   // oa系统职务id
      //   oaPositionId: null,   // oa系统职位id
      //   occurDate: null,  // 入职日期
      //   trialTerm: null,  // 试用期 字典StaffTrialLimit
      //   endDate: formatDate(new Date(), 'YYYY-mm-dd'),  // 实际转正日期
      //   remark: null,
      //   state: 2,           // 状态 -1草稿 0未开始 1进行中 2已完成 3已退回  4已取消 5已撤回
      //   priority: 0        // 优先级 0普通 1重要
      // },

      // dept: null,
      // post: null,
      // position: null,
      // dictMap: {},
      // staffList: []
    })
    // const ruleFormRef = ref({});
    // const formRules = reactive({
    //   staffId: {required: true, message: '请选择员工', trigger: 'change'},
    //     title: {required: true, message: '请填写主题', trigger: 'blur'},
    //     endDate: {required: true, message: '请选择实际转正日期', trigger: 'change'}
    // });

    let close = (formEl) => {
      context.emit("ChiShaiormClose");
      // resetForm(formEl)
      formEl.value.reset()
    };

    let submit = (formEl) => {
      formEl.value.submit()
      // state.formData.occurDate = format(state.occurDate, 'yyyy-MM-dd HH:mm')
      // state.formData.settlementDate = format(state.settlementDate, 'yyyy-MM-dd HH:mm')
      // state.form.id = idw.value;
      // if (!formEl) return;
      // await formEl.validate((valid, fields) => {
      //   if (valid) {
      //     console.log(state.formData);

      //     // state.formData.executorIds = JSON.stringify(state.formData.executorIds)
      //     homeCalendar.addOfficial(state.formData).then((res) => {
      //       if (res.code === 0) {
      //         resetForm(formEl)
      //         close(formEl)
      //       }
      //     });
      //   } else {
      //     console.log("error submit!", fields);
      //   }
      // });

      // console.log(state.formData);
      // state.form.id = idw.value
      // homeCalendar
      //   .modifyMatter(state.formData)
      //   .then((res) => {
      //       if(res.code===1){

      //       }
      //   });
    };
    // const resetForm = (formEl) => {
    //   if (!formEl) return
    //   formEl.resetFields()
    // }

    // const getStaffData = async () => {
    //   try {
    //     const res = await listByState('1')
    //     state.staffList = res.data
    //   } catch (e) {
    //     console.log(e)
    //   }
    // }
    // const staffChange = async (val) => {
    //   const res = await getStaffById(val)
    //   state.dept = res.data.dept
    //   if (state.dept) {
    //     state.formData.oaDeptId = state.dept.id
    //   }
    //   state.post = res.data.officePost
    //   if (state.post) {
    //     state.formData.postId = state.post.id
    //   }
    //   state.position = res.data.officePosition
    //   if (state.position) {
    //     state.formData.oaPositionId = state.position.id
    //   }
    //   state.formData.occurDate = res.data.joinedDate
    //   state.formData.trialTerm = res.data.trialDay
    // }
    const dateChange = val => {
      // 好像不需要这样, 事项是当前执行, 只不过转正时间非现在
      // if (val) {
      //   let today = new Date()
      //   let selectDate = new Date(val)
      //   state.formData.state = today.getTime() < selectDate.getTime() ? 0 : 2
      // }
    }
    onMounted(() => {
      // getDictByCodes(['StaffTrialLimit']).then(res => state.dictMap = res.data)
      // getStaffData()
      // if (state.formData.staffId) {
      //   staffChange(state.formData.staffId)
      // }
    })

    return {
      ...toRefs(state),
      // getStaffData,
      // staffChange,
      // dateChange,
      submit,
      close,
      form
      // formRules,
      // ruleFormRef,
      // resetForm,
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
